﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace QuantumEngine
{
    public partial class QEventEditor : Form
    {
        public static QEventEditor Active = null;
        public GameObj ActiveQuest
        {
            get
            {
                return mActiveQuest;
            }
            set
            {
                mActiveQuest = value;
                ActiveElement = null;
                UpdateEditor(true);

                
            }
        }
        private GameObj mActiveQuest;

        public GameObj ActiveElement = null;

        public Dictionary<TreeNode, GameObj> TreeLUT = new Dictionary<TreeNode, GameObj>();

        public QEventEditor()
        {
            InitializeComponent();
            Active = this;
        }

        public void SetActiveQuest(GameObj q)
        {
            ActiveQuest = q;

        }
        public void UpdateEditor(bool syncTree)
        {
            
            if (ActiveElement == null)
            {
                ActiveElement = ActiveQuest;
            }

            ClearEditor();

            if (ActiveElement == ActiveQuest)
            {

                var ne = new EditQuestRoot();
                ne.Active = ActiveElement;
                SetEditor(ne);
            }
            else
            {
                switch (ActiveElement.Kind)
                {
                    case QuestKind.Group:
                        AddQuestControls(ActiveElement);
                        break;
                    default:
                        EditorArea.Controls.Add(new Label() { Text = "No Editor!", Top = 40, Left = 40 });

                        break;
                }
            }
            if (syncTree)
            {
                UpdateTrees();
            }
        }
        public void AddQuestControls(GameObj o)
        {
            AddControls(5,5,o);
        }
        public TreeNode GetNode(GameObj o)
        {
            foreach (var k in TreeLUT.Keys)
            {
                if (TreeLUT[k] == o)
                {
                    return k;
                }
            }
            return null;
        }
        private void AddControls(float x,float y,GameObj o)
        {
            UserControl uc = null;
            var tn = GetNode(o);
            if (tn == null)
            {
                return;
            }
            if (tn.IsExpanded || tn.IsSelected)
            {
                EditorArea.Controls.Add(new Label() { Text = "Element:" + o.Name, Left = (int)x, Top = (int)y });
           

                y += 30;
                x += 10;

                switch (ActiveElement.Kind)
                {
                    case QuestKind.Converse:
                        break;
                }

                foreach (var o2 in o.Actors)
                {
                    AddControls(x + 50, y + (uc != null ? uc.Height + 25 : 25), o2);
                }
            }
            else
            {
                EditorArea.Controls.Add(new Label() { Text = "(Closed)Element:" + o.Name, Left = (int)x, Top = (int)y,Enabled =false });


            }
        }
        public void UpdateTrees()
        {
            QuestTree.Nodes.Clear();
            QuestTree.ShowRootLines = true;
            if (ActiveQuest == null)
            {
                QuestTree.Nodes.Add("No Quest!");
                return;
            }
            TreeLUT.Clear();
            var root = new TreeNode("Quest");
            QuestTree.Nodes.Add(root);
            AddQuestNode(ActiveQuest, root);
        }

        public void RegNode(TreeNode n, GameObj o)
        {
            if (TreeLUT.ContainsKey(n))
            {
                TreeLUT.Remove(n);
            }
            TreeLUT.Add(n, o);
        }
        public void SyncNodeName(GameObj o)
        {
            foreach (var k in TreeLUT.Keys)
            {
                if (TreeLUT[k] == o)
                {
                    k.Text = o.Name;
                }
            }
        }
        public GameObj GetObj(TreeNode n)
        {
            if (TreeLUT.ContainsKey(n) == false)
            {
                return null;
            }
            return TreeLUT[n];
        }

        public void AddQuestNode(GameObj q, TreeNode root)
        {
            TreeNode tn = new TreeNode(q.Name);
            root.Nodes.Add(tn);
            RegNode(tn, q);
            foreach (var o in q.Actors)
            {
                AddQuestNode(o, tn);
            }
        }
       
        public void ClearEditor()
        {
            
            EditorArea.Controls.Clear();

        }
        public void SetEditor(Control c)
        {
            EditorArea.Controls.Add(c);
        }
        private void BNewQuest_Click(object sender, EventArgs e)
        {
            if (GameManager.Active != null)
            {
                var nq = new GameObj();
                nq.Name = "(Quest) New";
                nq.Parent = GameManager.Active.QuestRoot;
                SetActiveQuest(nq);
            }
            else
            {
                
            }

        }

        private void QuestChecker_Tick(object sender, EventArgs e)
        {

        }
        public void RegType(QuestKind id)
        {
            QuestType.Items.Add(id);
        }
        private void QEventEditor_Load(object sender, EventArgs e)
        {
            RegType(QuestKind.Group);
            RegType(QuestKind.Start);
            RegType(QuestKind.Cinematic);
            RegType(QuestKind.Converse);
            RegType(QuestKind.Direction);
            RegType(QuestKind.Fin);
            RegType(QuestKind.Condtional);
        }

        private void QuestType_SelectedIndexChanged(object sender, EventArgs e)
        {
            var kind = (QuestKind)QuestType.SelectedItem;
            if (ActiveElement != null)
            {
                ActiveElement.Kind = kind;
            }
            UpdateEditor(false);
        }

        private void coolButton1_Click(object sender, EventArgs e)
        {
            var o = SpawnObj();
            if (ActiveElement != null)
            {
                ActiveElement.AddActor(o);
                UpdateEditor(true);
            }
        }


        public GameObj SpawnObj()
        {
            return new GameObj() { Name = "(Element) New" };
        }

        private void QuestTree_AfterSelect(object sender, TreeViewEventArgs e)
        {

            ActiveElement = GetObj(e.Node);
            if (ActiveElement != null)
            {
                QuestType.SelectedItem = ActiveElement.Kind;
                UpdateEditor(false);
            }

        }
   
    }
    public enum QuestKind{
        Group,Converse,Direction,Cinematic,Start,Fin,Condtional
    }
}
